package cn.tedu.redis.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class RedisConfiguration {
    //创建2个RedisTemplate对象 用不同的序列化器
    @Bean("redis01")
    public RedisTemplate initRedis01(RedisConnectionFactory factory){
        RedisTemplate redisTemplate=new RedisTemplate();
        redisTemplate.setConnectionFactory(factory);
        //自定义序列化 操作redis 操作key value 考虑hash key value field
        //key值必须在客户端传递String类型数据,然后通过底层对string字符串做序列化
        redisTemplate.setKeySerializer(RedisSerializer.string());
        //value必须是String类型,因为客户端要使用string的序列化
        redisTemplate.setValueSerializer(RedisSerializer.string());
        return redisTemplate;
    }
    @Bean("redis02")
    public RedisTemplate initRedis02(RedisConnectionFactory factory){
        RedisTemplate redisTemplate=new RedisTemplate();
        redisTemplate.setConnectionFactory(factory);
        //自定义序列化 操作redis 操作key value 考虑hash key value field
        //key值可以是java中任意Object类型,但是必须支持Searlizable接口
        redisTemplate.setKeySerializer(RedisSerializer.java());
        //value值可以是java中任意Object类型,但是必须支持Searlizable接口
        redisTemplate.setValueSerializer(RedisSerializer.java());
        return redisTemplate;
    }
}
